﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls.Ribbon;
using System.Windows.Forms;
using System.Windows.Input;
using AIPASoft.Interfaces.Base;
using AIPASoft.Interfaces.BusinessDomain;
using AIPASoft.Interfaces.Security;
using AIPASoft.SecurityLibrary.ViewModel;
using Application = System.Windows.Application;
using MessageBox = System.Windows.MessageBox;

namespace AIPASoft.SecurityLibrary.Model
{
    class MenuItemManage : ViewModelBase
    {
        private IApplicationManager _applicationManager;
        private ISecurityLibrary _securityLibrary;

        public MenuItemManage(IApplicationManager applicationManager,ISecurityLibrary securityLibrary)
        {
            _applicationManager = applicationManager;
            _securityLibrary = securityLibrary;
            InitializeMenuItems();
        }

        private List<RibbonApplicationMenuItem> _ribbonMenuItems;

        public List<RibbonApplicationMenuItem> RibbonMenuItems
        {
            get { return _ribbonMenuItems; }
            private set
            {
                _ribbonMenuItems = value;
                NotifyPropertyChanged("RibbonMenuItems");
            }
        }
         
        private void InitializeMenuItems()
        { 
            RibbonMenuItems=new List<RibbonApplicationMenuItem>();

            var mnuItem = new RibbonApplicationMenuItem
                {Command = ShowSecurityConsoleCommand, Header = "Security Console"};
            RibbonMenuItems.Add(mnuItem);


            RibbonMenuItems = new List<RibbonApplicationMenuItem>();
          mnuItem = new RibbonApplicationMenuItem { Command = ShowLoginCommand, Header = "Login/Logoff" };
            RibbonMenuItems.Add(mnuItem);

            mnuItem = new RibbonApplicationMenuItem { Command = ApplicationExitCommand, Header = "Exit"};
            RibbonMenuItems.Add(mnuItem);
        }


        private ICommand _showLoginCommand;

        private ICommand ShowLoginCommand
        {
            get
            {
                return _showLoginCommand ?? (_showLoginCommand = new RelayCommand(param => this.ShowLogin(), null
                                                                     ));
            }
        }


        private ICommand _showSecurityConsoleCommand;
        public ICommand ShowSecurityConsoleCommand
        {
            get
            {
                return _showSecurityConsoleCommand ??
                       (_showSecurityConsoleCommand = new RelayCommand(param => this.ShowSecurityConsole(), null));
            }
        }


        private ICommand _applicationExitCommand; 
        private ICommand ApplicationExitCommand
        {
            get
            {
                return _applicationExitCommand ?? (_applicationExitCommand = new RelayCommand(param => this.ApplicationExit(), null
                                                                     ));
            }
        }
         
        private void ShowSecurityConsole()
        {

        }

        private void ApplicationExit()
        {

            var res = MessageBox.Show("Do you want to exit ?", "", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (res == MessageBoxResult.Yes)
            Application.Current.Shutdown(0);
        }


        private LoginWindow _loginWindow;
        readonly LoginWindowViewModel _loginWindowViewModel = new LoginWindowViewModel();

        private void ShowLogin()
        { 

            _loginWindow = new LoginWindow()
            {
                WindowStyle = WindowStyle.SingleBorderWindow,
                WindowStartupLocation = WindowStartupLocation.CenterScreen,
                Title = "Login",
                DataContext = _loginWindowViewModel
            };

            _loginWindowViewModel.OnWindowClose += CloseLoginDialog;
            _loginWindow.Width = 320;
            _loginWindow.Height = 280;
            var res =_loginWindow.ShowDialog();
            if (res == true)
               _applicationManager.CurrentUser = _loginWindowViewModel.UserRelam ;

        } 
        private IUserRealm _userRealm;
        public IUserRealm UserRelam
        {

            get { return _userRealm; }
            set
            {
                _userRealm = value;
                NotifyPropertyChanged("UserRealm");
            }
        }


        private void CloseLoginDialog(object source, EventArgs e)
        { 
            _loginWindow.Close();
        }

        public void Dispose()
        {
            throw new NotImplementedException();
        }

    }
}
